<template>
  <div id="key-activity">
    <div id="key-activity-metro">
      <div
        class="metro-line-item"
        :class="{ active: index === LineIsActived }"
        v-for="(line, index) in AllLineData"
        :key="index"
        @click.stop="handleClick(line, index)"
      >
        <div
          class="key-activity-line-icon"
          :style="{
            'background-color': line.color,
            'border-color': line.color,
          }"
        ></div>
        <div class="key-activity-line-name" :style="{ color: line.color }">
          {{ line.name.slice(-3) }}
        </div>
      </div>
    </div>

    <div id="key-activity-content">
      <div class="header">
        <div class="title">重点活动</div>
        <div class="date">{{ date }}</div>
      </div>
      <div class="content">
        <div class="content-item" v-if="activityData.length > 0">
          <div
            class="item"
            v-for="item in activityData"
            :key="item.id"
            @click="AcitveClick(item)"
          >
            <div>{{ item.activedTime }}</div>
            <div>{{ item.activedPlace }}</div>
            <div>{{ item.activity }}</div>
          </div>
        </div>
        <a-empty v-else description="无数据" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, computed } from 'vue'
import { useLineData } from '@/store'
import { activity } from '@/store/staticData.js'
import dayjs from 'dayjs'
import {
  flyToTargetLine,
  flashHighlight,
  addAllCones,
  removeAllCones,
  flyToTargetCone,
} from '@/cesiumTools/effectController.js'
import { flyToDefaultView } from '@/cesiumTools/sceneManager'

// 获取线路数据
const lineData = useLineData()
const AllLineData = lineData.allData
//记录viewer实例
let viewer = null
//记录当前日期
const date = computed(() => {
  return dayjs().format('YYYY-MM-DD')
})
// 重点活动列表
const activityData = ref([])
//记录当前选中线路
const LineIsActived = ref(-1)
//记录选中线路站点数据
const LineStationData = ref([])

//加载时从pinia中获取vierwer实例
onMounted(() => {
  viewer = lineData.viewer

})

//点击加载活动列表
/*
 1. 点击飞行到目标线路，加载对应线路活动列表（mock数据）
 2. 高亮线路及站点数据
 3. 高亮活动列表项对应的站点数据
*/
const handleClick = (data, index) => {
  LineIsActived.value = index
  //根据活动列表中的线路名称过滤数据
  const filterData = activity.filter(
    (item) => item.activedLine === data.name.slice(-3)
  )
  activityData.value = filterData
  LineStationData.value = data.stationsList
  flyToTargetLine(viewer, data.name)
  flashHighlight(data.name)
  addCones(data.color)
}
//添加高亮圆柱
const addCones = (color) => {
  removeAllCones(viewer)
  /* 根据活动对应站点ID，过滤出对应ID的站点数据,并设置为name */
  // console.log(LineStationData.value,activityData.value)
  const stations = LineStationData.value
    .map((item, index) => {
      item.id = index
      return item
    })
    .filter((item) =>
      activityData.value.some((activity) => activity.id === item.id)
    )
  addAllCones(viewer, stations, color)
}
// 点击活动列表项，飞行到对应站点
const AcitveClick = (item) => {
  flyToTargetCone(viewer, item)
}

// 组件销毁时移除高亮线路及站点数据
onBeforeUnmount(() => {
  removeAllCones(viewer)

  //视角恢复默认
  flyToDefaultView(viewer)
})
</script>

<style scoped lang="scss">
#key-activity {
  pointer-events: all;
}
#key-activity-metro {
  position: absolute;
  bottom: 0;
  left: 34%;
  transform: translateX(-50%);
  width: 180px;
  height: 200px;
  background-color: rgba(0, 0, 0, 0.3);
  border: 1px solid #664a16;
  padding: 4px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-content: space-around;
  flex-wrap: wrap;
  flex: 1;
}

.metro-line-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
  margin-bottom: 4px;
  transition: all 0.3s ease-in-out;

  padding: 0 10px;
  cursor: pointer;
  &:hover {
    border: 1px solid #d8961a;
  }
  &.active {
    border: 1px solid #d8961a;
  }
}

.key-activity-line-icon {
  width: 10px;
  height: 10px;
  margin-right: 5px;
  cursor: pointer;
}
/* 活动内容样式 */
#key-activity-content {
  width: 550px;
  height: 200px;
  border: 1px solid #ab7818;
  background-color: rgba(0, 0, 0, 0.3);
  position: absolute;
  left: 39%;
  bottom: 0;
  color: #fff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.header {
  height: 30px;
  padding: 4px;
  background: rgb(255, 255, 255);
  background-image: linear-gradient(
    180deg,
    rgba(255, 255, 255, 1) 9%,
    rgba(211, 156, 50, 1) 57%
  );
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: -0.521vw;
  font-family: '等线Bold';
  display: flex;
}
.header > .title {
  margin-right: 10px;
}
.content {
  flex: 1;
}
.content-item {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100%;
}
.item {
  width: 150px;
  height: 80px;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;

  border: 1px solid #ab7818;
  background-color: rgba(214, 174, 41, 0.1);
  margin: 4px;
  padding: 2px;
  text-align: center;

  transition: all 0.3s linear;
  color: #37b3bb;
  font-size: 16px;

  pointer-events: all;
  cursor: pointer;
  &:hover {
    background-color: rgba(214, 174, 41, 0.3);
    color: #fff;
  }
}
</style>
